package JDBC;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class jdbc_study4 {
    public static void main(String[] args) {
        List<emp> list = findAll();
        for (emp emp : list) {
            System.out.println(emp);
        }
    }

    public static List<emp> findAll() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<emp> list = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
            String sql = "select * from emp";
            stmt = conn.createStatement();
            //执行sql了
            rs = stmt.executeQuery(sql);
            emp Emp = null;
            while (rs.next()) {
                int id = rs.getInt("id");
                String ename = rs.getString("ename");
                int job_id = rs.getInt("job_id");
                int mgr = rs.getInt("mgr");
                Date joinate = rs.getDate("joindate");
                double salary = rs.getDouble("salary");
                double bounds = rs.getDouble("bonus");
                int dept = rs.getInt("dept_id");
                Emp = new emp();
                Emp.setId(id);
                Emp.setEname(ename);
                Emp.setJob_id(job_id);
                Emp.setMgr(mgr);
                Emp.setJoinate(joinate);
                Emp.setSalary(salary);
                Emp.setBounds(bounds);
                Emp.setDept(dept);
                list.add(Emp);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
